package rulebase;

import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;

import managers.FeatureManager;

import features.DBFeatureValues;
import features.Feature;
import features.FeatureList;

public class FeaturesWindow extends JFrame 
{
	private JList FeaturesList;
	private JList SelectedFeaturesList;
	private JComboBox cbxType;
	private JComboBox cbxPOS;
	private JComboBox cbxFeature;
	private JButton btnOk;
	private JButton btnAddEditFeature;
	private DefaultListModel selectedFeatureModel;
	private DefaultListModel featureModel = new DefaultListModel();

	private ArrayList<Feature> selectedList = new ArrayList<>();
	
	private static LinkedHashMap<String, ArrayList<DBFeatureValues>> featureMap;
	
	
	public FeaturesWindow() 
	{
		getContentPane().setLayout(null);
		setVisible(true);
		setSize(500,400);
		setLocationRelativeTo(null);
		selectedFeatureModel = new DefaultListModel();
		initGUI();
	}
	
	public DefaultListModel getSelectedFeatureModel()
	{
		return selectedFeatureModel;
	}
	
	public void setSelectedFeatureModel(DefaultListModel selectedFeatureModel) 
	{
		this.selectedFeatureModel = selectedFeatureModel;
	}
	
	private void initGUI()
	{
		featureMap = FeatureManager.getFeatureMap();
		
		FeaturesList = new JList();
		
		SelectedFeaturesList = new JList();
		
		JLabel lblType = new JLabel("Type");
		lblType.setBounds(10, 11, 46, 14);
		getContentPane().add(lblType);
		
		JLabel lblPOS = new JLabel("POS");
		lblPOS.setBounds(10, 41, 46, 14);
		getContentPane().add(lblPOS);
		
		JLabel lblFeature = new JLabel("Feature");
		lblFeature.setBounds(10, 68, 70, 14);
		getContentPane().add(lblFeature);
		
		JLabel lblFeatures = new JLabel("Feature Values");
		lblFeatures.setBounds(10, 91, 200, 14);
		getContentPane().add(lblFeatures);
		
		JLabel lblSelectedFeatures = new JLabel("Selected Features");
		lblSelectedFeatures.setBounds(274, 91, 200, 14);
		getContentPane().add(lblSelectedFeatures);
		
		JScrollPane spFeaturesList = new JScrollPane();
		spFeaturesList.setBounds(16, 116, 200, 200);
		getContentPane().add(spFeaturesList);
		spFeaturesList.setViewportView(FeaturesList);
		
		JScrollPane spSelectedFeaturesList = new JScrollPane();
		spSelectedFeaturesList.setBounds(274, 116, 200, 200);
		getContentPane().add(spSelectedFeaturesList);
		spSelectedFeaturesList.setViewportView(SelectedFeaturesList);
		
		btnOk = new JButton("Ok");
		btnOk.setBounds(109, 327, 89, 23);
		getContentPane().add(btnOk);
		
		btnAddEditFeature = new JButton("Add/Edit");
		btnAddEditFeature.setBounds(10, 327, 89, 23);
		getContentPane().add(btnAddEditFeature);
		
		cbxType = new JComboBox();
		cbxType.setBounds(66, 8, 150, 20);
		getContentPane().add(cbxType);
		
		cbxPOS = new JComboBox();
		cbxPOS.setBounds(66, 38, 150, 20);
		getContentPane().add(cbxPOS);
		
		cbxFeature = new JComboBox();
		cbxFeature.setBounds(66, 68, 150, 20);
		getContentPane().add(cbxFeature);
		
		initCbxTypeItems();
		initCbxPOSItems();
	
	}
	
	private void initCbxTypeItems()
	{
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement("Word");
		model.addElement("Phrase");
		model.addElement("Clause");
		cbxType.setModel(model);
		
	}
	
	public void initCbxPOSItems()
	{
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		if(cbxType.getSelectedItem().equals("Word")){
			model.addElement("N");
			model.addElement("V");
			model.addElement("Adj");
			model.addElement("Adp");
			model.addElement("Adv");
			model.addElement("Conj");
			model.addElement("Particle");
			
			cbxPOS.setEnabled(true);
		}
		else if(cbxType.getSelectedItem().equals("Phrase")){
			model.addElement("NP");
			model.addElement("VP");
			model.addElement("AdjP");
			model.addElement("AdvP");
			
			cbxPOS.setEnabled(true);
		}	
		else if(cbxType.getSelectedItem().equals("Clause")){
			cbxPOS.setEnabled(false);
		}
		
		cbxPOS.setModel(model);
		setFeatureList();
		clearFeatureList();
		clearSelectedFeature();
	}
	
	public void setFeatureList(){	
		if(cbxType.getSelectedItem().equals("Word") || cbxType.getSelectedItem().equals("Phrase"))
			cbxFeature.setModel(initCbxFeatureItems(featureMap.get(cbxPOS.getSelectedItem())));
		
		else if(cbxType.getSelectedItem().equals("Clause"))
			cbxFeature.setModel(initCbxFeatureItems(featureMap.get("Cl")));
		
		clearFeatureList();
		clearSelectedFeature();
	}
	
	private DefaultComboBoxModel initCbxFeatureItems(ArrayList<DBFeatureValues> list){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		
		for(int i=0; i < list.size(); i++)
			model.addElement(list.get(i).getFeatureName());
		
		return model;
	}
	
	public void setFeatureJList(){
		ArrayList<String> featureList;
		featureModel = new DefaultListModel();
		if(!cbxType.getSelectedItem().equals("Clause"))
			featureList = featureMap.get(cbxPOS.getSelectedItem()).get(cbxFeature.getSelectedIndex()).getValues();
		else
			featureList = featureMap.get("Cl").get(cbxFeature.getSelectedIndex()).getValues();
		
		for(int i=0; i < featureList.size(); i++){
			featureModel.addElement(featureList.get(i));
		}
		
		FeaturesList.setModel(featureModel);
	}
	
	public void addSelectedFeature(){
		Feature feature = new Feature(cbxFeature.getSelectedItem().toString(), FeaturesList.getSelectedValue().toString(), false);
		
		selectedList.add(feature);
		selectedFeatureModel.addElement(FeaturesList.getSelectedValue());
		SelectedFeaturesList.setModel(selectedFeatureModel);
		
	}
	
	public void deleteSelectedFeature()
	{
		for(int i=0; i<selectedList.size(); i++){
			if(selectedList.get(i).getValue().equals(SelectedFeaturesList.getSelectedValue().toString()))
				selectedList.remove(i);
		}
		
		selectedFeatureModel.removeElement(SelectedFeaturesList.getSelectedValue());
	}
	
	public void clearFeatureList(){
		
		selectedFeatureModel.removeAllElements();
	}
	
	public void clearSelectedFeature(){
		
		featureModel.removeAllElements();
	}
	
	public ArrayList<Feature> getSelectedFeaturesList(){
		return selectedList;
	}
	
	public void setFeatureJListListener(MouseListener AL){
		FeaturesList.addMouseListener(AL);
	}
	
	public void setDeleteFeatureJListListener(MouseListener AL){
		SelectedFeaturesList.addMouseListener(AL);
	}
	
	public void setCbxFeatureTypeListener(ActionListener AL)
	{
		cbxType.addActionListener(AL);
	}
	
	public void setCbxPOSListener(ActionListener AL)
	{
		cbxPOS.addActionListener(AL);
	}
	
	public void setBtnOkListener(ActionListener AL)
	{
		btnOk.addActionListener(AL);
	}
	
	public void setBtnAddEditFeatureListener(ActionListener AL)
	{
		btnAddEditFeature.addActionListener(AL);
	}
	
	public void setCbxFeatureListener(ActionListener AL){
		cbxFeature.addActionListener(AL);
	}
	
	
}
